<template>
  <div class="attr-container">
    <el-card style="margin: 30px 0">
      <CategorySelect @get-attr="onGetAttr" />
    </el-card>
    <el-card>
      <div v-if="isShowAttrTable" class="attr-info">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin: 20px 0"
          :disabled="!attrInfo.categoryId"
          @click="showAddAttr"
          >添加属性</el-button
        >
        <el-table :data="attrList" style="width: 100%" border>
          <el-table-column type="index" label="序号" width="80" />
          <el-table-column prop="attrName" label="属性名称" width="width" />
          <el-table-column label="属性值列表" width="width">
            <template #default="{ row }">
              <el-tag
                v-for="item in row.attrValueList"
                :key="item.id"
                type="success"
                style="margin-right: 20px"
              >
                {{ item.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template #default="{ row }">
              <el-button
                icon="el-icon-edit"
                size="mini"
                type="primary"
                @click="onEdit(row)"
              />
              <el-button
                icon="el-icon-delete"
                size="mini"
                type="danger"
                @click="onDeleteAttr(row.id)"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-else class="operator-attr">
        <el-form ref="form" inline>
          <el-form-item label="属性名">
            <el-input v-model="attrInfo.attrName" />
          </el-form-item>
        </el-form>
        <div class="action1">
          <el-button
            type="primary"
            icon="el-icon-plus"
            :disabled="!attrInfo.attrName"
            @click="onAddAttr"
            >添加属性值</el-button
          >
        </div>
        <el-table
          :data="attrInfo.attrValueList"
          style="width: 100%; margin-top: 20px"
          border
        >
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column label="属性值名称" width="0" />
          <el-table-column label="属性值名称">
            <template #default="{ row }">
              <el-input v-model="row.valueName" placeholder="请输入属性名" />
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="{ $index }">
              <el-button
                icon="el-icon-delete"
                size="mini"
                type="danger"
                @click="onDelete($index)"
              />
            </template>
          </el-table-column>
        </el-table>
        <div class="action2" style="margin-top: 20px">
          <el-button
            type="primary"
            :disabled="attrInfo.attrValueList.length === 0"
            @click="onSave"
            >保存</el-button
          >
          <el-button @click="onCancel">取消</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import CategorySelect from "components/categorySelector";
import { cloneDeep } from "lodash";
export default {
  name: "AttrManage",
  components: { CategorySelect },
  props: {},
  data() {
    return {
      attrList: [],
      // 属性表格与添加/编辑属性的显示与隐藏
      isShowAttrTable: true,
      // 属性信息
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: "",
        categoryLevel: 3,
        id: "",
      },
      action: "add",
      categoryIds: {},
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 获取属性
    async onGetAttr(categoryIds) {
      if (Object.keys(this.categoryIds).length === 0) {
        this.categoryIds = categoryIds;
        this.attrInfo.categoryId = categoryIds.category3Id;
      }
      const res = await this.$api.attr.getAttrByCategoryId(this.categoryIds);
      if (res.code === 200) {
        this.attrList = res.data;
      }
    },
    onAddAttr() {
      this.attrInfo.attrValueList.push({
        attrId: undefined,
        valueName: "",
      });
    },
    onDelete(index) {
      this.attrInfo.attrValueList.splice(index, 1);
    },
    // 更新属性信息
    async onSave() {
      const res = await this.$api.attr.addOrUpdateAttr(this.attrInfo);
      if (res.code === 200) {
        this.$message.success(
          this.action === "add" ? "添加属性成功" : "更新属性成功"
        );
        this.resetAttrInfo();
        this.isShowAttrTable = true;
        this.onGetAttr();
      } else {
        this.$message.warning(res.message);
      }
    },
    // 编辑
    async onEdit(row) {
      this.action = "edit";
      this.attrInfo = cloneDeep(row);
      this.isShowAttrTable = false;
    },
    // 删除属性
    async onDeleteAttr(attrId) {
      try {
        await this.$confirm("删除之后不课恢复,确定要删除吗", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        });
        const res = await this.$api.attr.deleteAttrById(attrId);
        if (res.code === 200) {
          this.$message.success("删除成功");
          this.onGetAttr();
        } else {
          this.$message.error(res.message);
        }
      } catch (error) {
        if (error === "cancel") {
        } else {
          this.$message.error(error.message);
        }
      }
    },
    showAddAttr() {
      this.isShowAttrTable = false;
    },
    onCancel() {
      this.resetAttrInfo();
      this.isShowAttrTable = true;
    },
    resetAttrInfo() {
      const { categoryId } = this.attrInfo;
      Object.assign(this.attrInfo, this.$options.data().attrInfo);
      this.attrInfo.categoryId = categoryId;
    },
  },
};
</script>

<style scoped lang="less"></style>
